

//
//  UILabel+Extension.swift
//  Sina
//
//  Created by YYY on 2017/7/27.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

//import Foundation
import UIKit

extension UILabel
{
  
    convenience init(frame: CGRect,text: String,textColor: UIColor,fontsize: CGFloat)
    {
        self.init(frame: frame)
        self.text = text
        self.textColor = textColor
        self.font = UIFont.systemFont(ofSize: fontsize)
    }
}

extension UILabel
{
    //计算label宽度
    func getLabelWidth(text: String,font: UIFont,height: CGFloat) -> CGFloat
    {
        let textString = text as NSString
        let attributes = [NSFontAttributeName: font]
        
        let size = CGSize(width: 1000, height: height)
        let stringSize = textString.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
        return stringSize.width
    }
    //计算label高度
    func getLabelHeight(text: String,font: UIFont,width: CGFloat) -> CGFloat
    {
        let textString = text as NSString
        let attributes = [NSFontAttributeName: font]
        
        let size = CGSize(width: width, height: 1000)
        let stringSize = textString.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
        return stringSize.height
    }
}
